<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户重置密码</title>
    <script src="../static/js/jquery.min.js"></script>
</head>
<body>
<dev align="center">
    <h2>用户重置密码</h2>
    <table align="center">
        <tr>
            <td><span id="errorTips" style="color: red"></span></td>
            <td><span id="passTips" style="color: green"></span></td>
        </tr>
        <tr>
            <td>用户名： <input type="text" id="username"></td>
        </tr>
        <tr>
            <td>新密码： <input type="text" id="pwd"></td>
        </tr>
        <tr>
            <td>确认密码： <input type="text" id="qpwd"></td>
        </tr>
        <tr>
            <td>用户手机号:
                <input type="text" id="mobile">
            </td>
        </tr>
        <tr>
            <td>验证码:
                <input type="text" id="code">
            </td>
            <td>
                <button id="getCode">-获取验证码-</button>
            </td>
        </tr>
        <tr>
            <td>
                <button id="submit">重置密码</button>
            </td>
        </tr>
    </table>
</dev>
</body>
<script type="text/javascript">
    $(function () {



        $("#submit").click(function () {
            var fcode = $("#code").val();
            var username = $("#username").val();
            var pwd = $("#pwd").val();
            var qpwd = $("#qpwd").val();
            var mobile = $("#mobile").val();
            if (fcode != null && fcode != ''){
                if (fcode == code) {
                    if (pwd != null && pwd != ''){
                        if (pwd == qpwd){
                            if (mobile != null && mobile != '' && mobile.length == 11){
                                if (username != null && username != ''){
                                    $("#errorTips").text('');
                                    var data = {
                                        'username':username,
                                        'pwd':pwd,
                                        'mobile':mobile
                                    };
                                    $.ajax({
                                        url: '/api/user/reset/password',
                                        type: 'POST',
                                        data: JSON.stringify(data),
                                        dataType: 'JSON',
                                        contentType : "application/json",
                                        success: function (res) {
                                            if (res.code == 100){
                                                alert("修改成功");
                                                window.location.href = "/api/account/index";
                                            }else{
                                                alert(res.message);
                                            }
                                        }
                                    })
                                } else{
                                    $("#errorTips").text('用户名不得为空');
                                }
                            }else{
                                $("#errorTips").text('手机号不正确');
                            }
                        }else{
                            $("#errorTips").text('两次密码不相等');
                        }
                    } else{
                        $("#errorTips").text('密码不得为空');
                    }
                }else{
                    $("#errorTips").text('验证码不正确');
                }
            }else{
                $("#errorTips").text('验证码不正确');
            }
        });
        var code = '';

        $("#getCode").click(function () {
            var mobile = $("#mobile").val();
            if (mobile != null && mobile != '' && mobile.length == 11) {
                $("#errorTips").text('');
                $("#getCode").attr('disabled','disabled');
                var count = 60;
                timer =  setInterval(function () {
                    if (count > 0){
                        $("#getCode").text('-获取验证码-'+(--count));
                    }
                    else {
                        $("#getCode").text('-获取验证码-');
                        $("#getCode").removeAttr('disabled','disabled');
                        clearInterval(timer);
                    }
                },1000);
                $.ajax({
                    url:'/api/sms/sendSms/'+mobile,
                    type: 'get',
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 100) {
                            $("#passTips").text(res.message);
                            code = res.data;
                        }else{
                            $("#errorTips").text(res.message);
                        }
                    }
                })
            }else {
                $("#errorTips").text('手机号不正确');
            }
        })
    })
</script>
</html>